package com.ruoyi.web.controller.shop.imgs.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.shop.imgs.domain.ShopImages;
import com.ruoyi.web.controller.shop.imgs.service.IShopImagesService;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.ruoyi.web.utils.initVodClient.getPlayInfo;
import static com.ruoyi.web.utils.initVodClient.initVodClient;

/**
 * 店铺图片Controller
 * 
 * @author ruoyi
 * @date 2020-10-17
 */
@RestController
@RequestMapping("/images")
public class ShopImagesController extends BaseController
{
    @Autowired
    private IShopImagesService shopImagesService;

    /**
     * 查询店铺图片列表
     */
//    @PreAuthorize("@ss.hasPermi('shop:images:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopImages shopImages) throws Exception {
        startPage();
        List<ShopImages> list = shopImagesService.selectShopImagesList(shopImages);
        if (shopImages.getAddress()==5||shopImages.getAddress()==6||shopImages.getAddress()==7){
            if (list.size()>0){
                for (ShopImages si:list){
                    String video =si.getImgurl();
                    DefaultAcsClient client = initVodClient();
                    GetPlayInfoResponse response = new GetPlayInfoResponse();

                    response = getPlayInfo(client,video);
                    List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
                    //播放地址
                    for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                        System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
//                video.setUrl(playInfo.getPlayURL());
//                cs.setImgHead(playInfo.getPlayURL());
                        si.setImgurl(playInfo.getPlayURL());
                    }
                }
            }
        }
        return getDataTable(list);
    }
//
//    /**
//     * 导出店铺图片列表
//     */
//    @PreAuthorize("@ss.hasPermi('shop:images:export')")
//    @Log(title = "店铺图片", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ShopImages shopImages) throws IOException
//    {
//        List<ShopImages> list = shopImagesService.selectShopImagesList(shopImages);
//        ExcelUtil<ShopImages> util = new ExcelUtil<ShopImages>(ShopImages.class);
//        util.exportExcel(response, list, "images");
//    }

    /**
     * 获取店铺图片详细信息
     */
//    @PreAuthorize("@ss.hasPermi('shop:images:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(shopImagesService.selectShopImagesById(id));
    }

    /**
     * 新增店铺图片
     */
//    @PreAuthorize("@ss.hasPermi('shop:images:add')")
    @Log(title = "店铺图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopImages shopImages)
    {
        return toAjax(shopImagesService.insertShopImages(shopImages));
    }

    /**
     * 修改店铺图片
     */
//    @PreAuthorize("@ss.hasPermi('shop:images:edit')")
    @Log(title = "店铺图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopImages shopImages)
    {
        return toAjax(shopImagesService.updateShopImages(shopImages));
    }

    /**
     * 删除店铺图片
     */
//    @PreAuthorize("@ss.hasPermi('shop:images:remove')")
    @Log(title = "店铺图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(shopImagesService.deleteShopImagesByIds(ids));
    }
}
